package com.act.web;

import com.act.domain.AttendanceManagement;
import com.act.dto.PagerDTO;
import com.act.dto.Response;
import com.act.service.api.IAttendanceManagementService;
import com.act.util.LoggerUtil;
import com.act.vo.PagerVo;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("am")
public class AttendanceManagementController {
	@Resource
	private  IAttendanceManagementService iAttendanceManagementService;

	private final Logger LOGGER = Logger.getLogger(AttendanceManagementController.class);


	@RequestMapping(value = "/add",method = RequestMethod.POST,consumes = "application/json")
	@ResponseBody
	public Response add(@RequestBody List<AttendanceManagement> attendanceManagements) {
		try {
			for (AttendanceManagement attendanceManagement : attendanceManagements) {
				iAttendanceManagementService.add(attendanceManagement);
			}
			return Response.success();
		} catch (Exception e) {

			return Response.fail();
		}
	}

	@RequestMapping(value = "/delete",method = RequestMethod.POST)
	@ResponseBody
	public Response delete(int  id) {
		try {
			iAttendanceManagementService.delete(id);
			return Response.success();
		} catch (Exception e) {

			return Response.fail();
		}

	}


	@RequestMapping(value="/get",method=RequestMethod.POST)
	@ResponseBody
	public  Response getAll(@RequestBody PagerDTO pagerDTO){
		try {
			PagerVo<Map> amlist=iAttendanceManagementService.getAllList(pagerDTO);

			return Response.success(amlist);
		} catch (Exception e) {
			LoggerUtil.error(LOGGER, e);
		}
		return Response.fail();

	}

	@RequestMapping(value ="/getAttendByMonth/{empId}/{month}",method=RequestMethod.GET)
	public Response getList(@PathVariable int empId,@PathVariable int month) {
		try {
			List<Map>  Attends = iAttendanceManagementService.getConStatusByEidAndMonth(empId, month);
			return Response.success(Attends);
		} catch (Exception e) {
			LoggerUtil.error(LOGGER, e);
		}
		return Response.fail();
	}


	@RequestMapping(value ="/getAttendanceByDateTimeOrName/{year}/{month}/{jobnum}",method=RequestMethod.GET)
	@ResponseBody
	public Response  getAttendanceByDateTimeOrName(@PathVariable int year,@PathVariable int month,@PathVariable String jobnum) {
		try {
			List<AttendanceManagement>  Attends = iAttendanceManagementService.getAttendanceByDateTimeOrName(year,month,jobnum);
			return Response.success(Attends);
		} catch (Exception e) {
			LoggerUtil.error(LOGGER, e);
		}
		return Response.fail();

	}

	@RequestMapping(value ="/getCounts",method=RequestMethod.GET)
	public Response Test() {
		try {
			int i= iAttendanceManagementService.getCounts();
			return Response.success(i);
		} catch (Exception e) {
			LoggerUtil.error(LOGGER, e);
		}
		return Response.fail();
	}
	
	@RequestMapping(value ="/getInMonth/{jobnum}",method=RequestMethod.GET)
	@ResponseBody	
	public Response getByJobnumInMonth(@PathVariable String jobnum) {
		try {
			List<AttendanceManagement> attendanceManagements= iAttendanceManagementService.getByJobnumInMonth(jobnum);
			return Response.success(attendanceManagements);
		} catch (Exception e) {
			LoggerUtil.error(LOGGER, e);
		}
		return Response.fail();
	}
	
	
	@RequestMapping(value = "/add1",method = RequestMethod.POST)
	@ResponseBody
	public Response add(AttendanceManagement attendanceManagement) {
		try {
				iAttendanceManagementService.add(attendanceManagement);
			 return Response.success();
		} catch (Exception e) {

			return Response.fail();
		}
	}
}
